package service

import (
	"context"
	"example.com/m/v2/internal/domain/account_aggregate/entity"
	"example.com/m/v2/internal/domain/account_aggregate/repository"
	"fmt"
	"log"
)

// 感觉用不用接口都一样

type AccountDomainService struct {
	accountRepo    repository.MainAccountRepository
	subAccountRepo repository.SubAccountRepository
}

func NewAccountDomainService(accountRepo repository.MainAccountRepository, subAccountRepo repository.SubAccountRepository) *AccountDomainService {
	log.Printf("account domain service constructed")
	return &AccountDomainService{
		accountRepo:    accountRepo,
		subAccountRepo: subAccountRepo,
	}
}

func (domainService *AccountDomainService) FindMainAccount(ctx context.Context, mainAccountId uint) (*entity.MainAccountEntity, error) {
	account, err := domainService.accountRepo.FindById(ctx, mainAccountId)
	if err != nil {
		fmt.Printf("error:%v\n", err)
		return nil, err
	}
	return account, nil
}

func (domainService *AccountDomainService) CreateSubAccount(ctx context.Context, mainAccountId uint, appId uint) (*entity.SubAccountEntity, error) {
	subAccount := entity.NewSubAccountEntity(appId, mainAccountId, "default_nickname", "default_avatar")
	newEntity, err := domainService.subAccountRepo.Save(ctx, subAccount)
	if err != nil {
		return nil, err
	}
	return newEntity, nil
}
